package com.代理模式.静态代理.分库分表;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/1 9:36
 * @description:
 * @version:
 */
//切换数据源的静态代理
public class OrderServiceStaticProxy implements OrderService {
    private OrderService orderService;
    public OrderServiceStaticProxy(OrderService orderService) {
        this.orderService = orderService;
    }
    public int createOrder(Order order) {
        before();
        Long createTime = order.getCreateTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Integer routeNumber = Integer.valueOf(sdf.format(new Date(createTime)));
        System.out.println("静态代理自动分配到 DB_" + routeNumber + " 数据源处理数据");
        DynamicDataSourceEntry.set(routeNumber);
        this.orderService.createOrder(order);
        after();
        return 0;
    }
    private void after() {
        System.out.println("Proxy after method");
    }
    private void before() {
        System.out.println("Proxy before method");
    }
}

